package com.zboin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zboin.common.domain.AjaxResult;
import com.zboin.common.domain.PageResult;
import com.zboin.domain.Article;
import com.zboin.domain.ArticleType;
import com.zboin.mapper.ArticleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author zhboom
 * @date 2025/5/17 14:33
 */
@Slf4j
@Service
public class ArticleService {

    private final ArticleMapper articleMapper;
    private final ArticleTypeService articleTypeService;
    private final FileManageService fileManageService;

    public ArticleService(ArticleMapper articleMapper, ArticleTypeService articleTypeService, FileManageService fileManageService) {
        this.articleMapper = articleMapper;
        this.articleTypeService = articleTypeService;
        this.fileManageService = fileManageService;
    }

    public AjaxResult getArticleDetail(Integer id) {
        Article article = articleMapper.selectById(id);
        return article == null ? AjaxResult.error() : AjaxResult.success(article);
    }

    public AjaxResult getArticleContent(Integer id) {
        Article article = articleMapper.selectById(id);
        byte[] content = fileManageService.getFileBytes(article.getUrl());
        if (content.length > 0) {
            return AjaxResult.success("操作成功", new String(content));
        }
        return AjaxResult.error();
    }

    public AjaxResult getArticleListByType(Integer typeId) {
        ArticleType type = articleTypeService.getTypeDetail(typeId);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type.getName());
        wrapper.orderByDesc("update_date");
        return AjaxResult.success(articleMapper.selectList(wrapper));
    }

    public AjaxResult addArticle(Article article) {
        int update = articleMapper.insert(article);
        if (article.getType() != null) {
            articleTypeService.incrCount(article.getType(), article.getAuthor());
        }
        return AjaxResult.toAjaxResult(update);
    }

    public AjaxResult updateArticle(Article article) {
        Article old = articleMapper.selectById(article.getId());
        // update image, delete old image
        if (StringUtils.hasText(old.getImageUrl()) && !old.getImageUrl().equals(article.getImageUrl())) {
            fileManageService.deleteFile(old.getImageUrl());
        }
        return AjaxResult.toAjaxResult(articleMapper.updateById(article));
    }

    public PageResult<Article> list(Integer pageNum, Integer pageSize, Article article) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_date");
        Page<Article> page = new Page<>(pageNum, pageSize);
        Page<Article> articlePage = articleMapper.selectPage(page, wrapper);
        return PageResult.success(articlePage.getRecords(), articlePage.getTotal());
    }

    public AjaxResult deleteArticle(Integer id) {
        return AjaxResult.toAjaxResult(articleMapper.deleteById(id));
    }
}
